
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册账号 - 知聊 BBS论坛</title>
    <link rel="stylesheet" href="static/login.css">
    <link rel="stylesheet" href="static/register.css">
</head>
<body>
<div class="login-container">
    <h2>注册新账号</h2>
    <form method="POST" action="" onsubmit="return validateForm()">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username"
                   placeholder="4-20位字母/数字组合"
                   pattern="[a-zA-Z0-9]{4,20}"
                   required autofocus>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password"
                   placeholder="至少6位密码"
                   minlength="6"
                   required>
            <div class="password-strength">密码强度：<span id="strength-text">-</span></div>
        </div>

        <div class="form-group">
            <label for="password_verify">确认密码</label>
            <input type="password" id="password_verify" name="password_verify"
                   placeholder="再次输入密码"
                   required>
            <div class="password-match">✓ 密码匹配</div>
            <div class="password-mismatch">✗ 密码不匹配</div>
        </div>

        <button type="submit" class="register-btn">立即注册</button>
    </form>

    <div class="footer-links">
        <a href="login.php">已有账号？立即登录</a>
    </div>
</div>

<script>
    // 实时密码验证
    const password = document.getElementById('password');
    const passwordVerify = document.getElementById('password_verify');
    const matchMsg = document.querySelector('.password-match');
    const mismatchMsg = document.querySelector('.password-mismatch');

    function checkPasswordMatch() {
        if (password.value && passwordVerify.value) {
            if (password.value === passwordVerify.value) {
                matchMsg.style.display = 'block';
                mismatchMsg.style.display = 'none';
            } else {
                matchMsg.style.display = 'none';
                mismatchMsg.style.display = 'block';
            }
        }
    }

    password.addEventListener('input', checkPasswordMatch);
    passwordVerify.addEventListener('input', checkPasswordMatch);

    // 密码强度检测
    password.addEventListener('input', function() {
        const strengthText = document.getElementById('strength-text');
        const strength = calculateStrength(this.value);
        strengthText.textContent = strength.text;
        strengthText.style.color = strength.color;
    });

    function calculateStrength(pw) {
        const hasLower = /[a-z]/.test(pw);
        const hasUpper = /[A-Z]/.test(pw);
        const hasNumber = /\d/.test(pw);
        const hasSpecial = /[!@#$%^&*]/.test(pw);

        let score = 0;
        if (pw.length >= 6) score++;
        if (pw.length >= 8) score++;
        if (hasLower && hasUpper) score++;
        if (hasNumber) score++;
        if (hasSpecial) score++;

        switch(score) {
            case 0: case 1:
                return {text: '弱', color: '#e53e3e'};
            case 2: case 3:
                return {text: '中', color: '#d69e2e'};
            default:
                return {text: '强', color: '#38a169'};
        }
    }

    function validateForm() {
        if (password.value !== passwordVerify.value) {
            alert('两次输入的密码不一致！');
            return false;
        }
        return true;
    }
</script>
</body>
</html>
<?php
if (!empty($_POST['username']) and !empty($_POST['password'])) {
    include 'conn.php';

    $username = htmlspecialchars($_POST['username']);
    $password = $_POST['password'];
    $sql1 = "select username from users where username = ?";
    $stmt1 = $conn->prepare($sql1);
    $stmt1->bind_param("s", $username);
    $stmt1->execute();
    $row = $stmt1->get_result();
//判断是否存在相同的用户名
    if ($row->num_rows > 0) {
        die("<script>alert('用户已存在！')</script>");
    }
    $sql = "INSERT INTO users (username, password, level) VALUES (?,  ?, 1);";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $username, $password);

    if ($stmt->execute() === true) {
        header('Location: login.php');
    } else {
        echo "<script>alert('注册失败，请联系管理员')</script>";
    }
    $conn->close();
}